package servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 页面跳转Servlet，负责根据用户角色分发不同页面
 */
@WebServlet("/page/*")
public class PageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pathInfo = request.getPathInfo();
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");
        boolean isLoggedIn = (session.getAttribute("userId") != null);

        // 未登录用户仅允许访问登录页
        if (!isLoggedIn) {
            if ("/login".equals(pathInfo)) {
                forwardToPage(request, response, "login.html");
            } else {
                response.sendRedirect(request.getContextPath() + "/login");
            }
            return;
        }

        // 根据用户角色分发页面
        if (role == null) {
            response.sendRedirect(request.getContextPath() + "/login");
            return;
        }

        switch (role) {
            case "CONSUMER":
                handleConsumerPages(pathInfo, request, response);
                break;
            case "CUSTOMER_SERVICE":
                handleCustomerServicePages(pathInfo, request, response);
                break;
            case "OPERATOR":
                handleOperatorPages(pathInfo, request, response);
                break;
            default:
                response.sendRedirect(request.getContextPath() + "/page/login");
        }
    }

    /**
     * 处理消费者角色页面请求
     */
    private void handleConsumerPages(String pathInfo, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (pathInfo == null || "/".equals(pathInfo) || "/consumer".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/index.html");
        } else if ("/consumer/index".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/index.html");
        } else if ("/consumer/applicationForm".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/applicationForm.html");
        } else if ("/consumer/applicationList".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/applicationList.html");
        } else if ("/consumer/applicationDetail".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/applicationDetail.html");
        } else if ("/consumer/notifications".equals(pathInfo)) {
            forwardToPage(request, response, "consumer/notifications.html");
        } else if ("/consumer/applicationEdit".equals(pathInfo)){
            forwardToPage(request, response, "consumer/applicationEdit.html");
        } else {
            response.sendRedirect(request.getContextPath() + "/page/consumer");
        }
    }

    /**
     * 处理客服角色页面请求
     */
    private void handleCustomerServicePages(String pathInfo, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (pathInfo == null || "/".equals(pathInfo) || "/customerService".equals(pathInfo)) {
            forwardToPage(request, response, "customerService/index.html");
        } else if ("/customerService/index".equals(pathInfo)) {
            forwardToPage(request, response, "customerService/index.html");
        } else if ("/customerService/pending-audit".equals(pathInfo)) {
            forwardToPage(request, response, "customerService/pending-audit.html");
        }else if ("/customerService/processApplication".equals(pathInfo)){
            forwardToPage(request, response, "customerService/process-application.html");
        }
        else if ("/customerService/workOrder".equals(pathInfo)) {
            forwardToPage(request, response, "customerService/workOrder.html");
        } else if ("/customerService/applicationDetail".equals(pathInfo)) {
            forwardToPage(request, response, "customerService/application-detail.html");
        } else {
            response.sendRedirect(request.getContextPath() + "/page/customerService");
        }
    }

    /**
     * 处理运营角色页面请求
     */
    private void handleOperatorPages(String pathInfo, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (pathInfo == null || "/".equals(pathInfo) || "/operator".equals(pathInfo)) {
            forwardToPage(request, response, "operator/index.html");
        } else if ("/operator/ruleManagement".equals(pathInfo)) {
            forwardToPage(request, response, "operator/rule-management.html");
        } else if ("/operator/statistics".equals(pathInfo)) {
            forwardToPage(request, response, "operator/statistics.html");
        } else {
            response.sendRedirect(request.getContextPath() + "/page/operator");
        }
    }

    /**
     * 转发到指定页面
     */
    private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String page) {
        try {
            request.getRequestDispatcher("/WEB-INF/pages/" + page).forward(request, response);
        } catch (Exception e) {
            try {
                response.sendRedirect(request.getContextPath() + "/login");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
